package com.zzl.bonds.ticket.service.common.impl;

import com.zzl.bonds.ticket.constant.status.HandStatusEnum;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.constant.type.OperateButtonEnum;
import com.zzl.bonds.ticket.dto.KeyValue;
import com.zzl.bonds.ticket.dto.finance.SupplierCreditListDto;
import com.zzl.bonds.ticket.dto.finance.TicketFinanceListDto;
import com.zzl.bonds.ticket.exception.SysExceptionFactory;
import com.zzl.bonds.ticket.service.common.TicketFinanceHandService;
import com.zzl.bonds.ticket.untils.TicketUtils;
import com.zzl.general.common.exception.SysException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.zzl.bonds.ticket.constant.type.MenuButtonEnum.*;
import static com.zzl.bonds.ticket.exception.SysExceptionEnum.TICKET_FINANCE_RESULT_NOT_FOUND;

/**
 * @author gull
 * @date 2022/3/2 20:55:35
 */
@Service
public class TicketFinanceHandServiceImpl implements TicketFinanceHandService {

    @Value("#{'${config.buttonAuth}'.equals('off')}")
    private boolean buttonAuth;


    /**
     * 设置融资列表票据处理情况及气泡展示内容
     *
     * @param ticketList 票据集合
     * @throws SysException 当ticket对象没有规定的方法时抛出异常
     */
    @Override
    public void setFinanceTicketHandleSituation(List<?> ticketList) throws SysException {
        if (!CollectionUtils.isEmpty(ticketList)) {
            for (Object ticket : ticketList) {
                this.setFinanceOneTicketHandleSituation(ticket);
            }
        }
    }

    /**
     * 设置融资列表票据处理情况及气泡展示内容
     * 根据融资审核状态判断
     *
     * @param ticket 票据集合
     * @throws SysException 当ticket对象没有规定的方法时抛出异常
     */
    @Override
    public void setFinanceOneTicketHandleSituation(Object ticket) throws SysException {
        TicketStatusEnum ticketStatusEnum = TicketUtils.getTicketStatus(ticket);
        if (ObjectUtils.isEmpty(ticketStatusEnum)) {
            throw SysExceptionFactory.createSysException(TICKET_FINANCE_RESULT_NOT_FOUND);
        }
        Integer code = ticketStatusEnum.getCode();
        //失败
        if (TicketStatusEnum.REJECT_LIST.contains(code)) {
            TicketUtils.setHandStatus(ticket, HandStatusEnum.FAIL.getName());
            TicketUtils.setHandStatusInfo(ticket, TicketStatusEnum.getStatusHandInfo(code));
        } else if (TicketStatusEnum.FINANCE_ING_LIST.contains(code)) {
            //融资中
            TicketUtils.setHandStatus(ticket, "融资中");
        } else if (TicketStatusEnum.FINANCE_SUCCESS_LIST.contains(code)) {
            //成功
            TicketUtils.setHandStatus(ticket, HandStatusEnum.SUCCESS.getName());
        } else {
            //未知
            TicketUtils.setHandStatus(ticket, "未知");
        }
        TicketUtils.setHandStatusInfo(ticket, TicketStatusEnum.getStatusHandInfo(code));
        TicketUtils.setHandProgress(ticket, TicketStatusEnum.getProgress(code));

    }

    /**
     * 设置 融资复核按钮操作
     *
     * @param ticketCheckList 融资复核按钮
     */
    @Override
    public void setFinanceTicketCheckOperates(List<TicketFinanceListDto> ticketCheckList, HttpSession session) {
        boolean buttonAuthFlag = TicketUtils.getButtonAuth(session, FINANCE_CHECK_BUTTON.getCode());
        if ((buttonAuth || buttonAuthFlag) && !CollectionUtils.isEmpty(ticketCheckList)) {
            for (TicketFinanceListDto checkPage : ticketCheckList) {
                checkPage.setOperateButton(Collections.singletonList(new KeyValue(OperateButtonEnum.CHECK.getCode(), OperateButtonEnum.CHECK.getValue())));
            }
        }

    }

    /**
     * 设置 融资审批按钮操作
     *
     * @param ticketCheckList 融资审批按钮
     */
    @Override
    public void setFinanceTicketApproveOperates(List<TicketFinanceListDto> ticketCheckList, HttpSession session) {
        boolean buttonAuthFlag = TicketUtils.getButtonAuth(session, FINANCE_APPROVE_BUTTON.getCode());
        if ((buttonAuth || buttonAuthFlag) && !CollectionUtils.isEmpty(ticketCheckList)) {
            for (TicketFinanceListDto checkPage : ticketCheckList) {
                checkPage.setOperateButton(Collections.singletonList(new KeyValue(OperateButtonEnum.APPROVE_FINANCE.getCode(), OperateButtonEnum.APPROVE_FINANCE.getValue())));
            }
        }
    }

    /**
     * 设置 融资签署按钮操作
     *
     * @param ticketCheckList 融资签署按钮
     */
    @Override
    public void setFinanceTicketSignOperates(List<TicketFinanceListDto> ticketCheckList, HttpSession session) {
        boolean buttonAuthFlag = TicketUtils.getButtonAuth(session, FINANCE_SIGN_BUTTON.getCode());
        if ((buttonAuth || buttonAuthFlag) && !CollectionUtils.isEmpty(ticketCheckList)) {
            for (TicketFinanceListDto checkPage : ticketCheckList) {
                checkPage.setOperateButton(Collections.singletonList(new KeyValue(OperateButtonEnum.SIGN_FINANCE.getCode(), OperateButtonEnum.SIGN_FINANCE.getValue())));
            }
        }
    }

    /**
     * 设置 额度管理-融资额度列表按钮
     *
     * @param supplierCreditList 融资额度修改和详情按钮
     */
    @Override
    public void setFinanceCreditOperates(List<SupplierCreditListDto> supplierCreditList, HttpSession session) {
        boolean buttonAuthFlag = TicketUtils.getButtonAuth(session, FINANCE_CREDIT_UPDATE_BUTTON.getCode());
        List<KeyValue> operateButton = new ArrayList<>(2);
        //修改按钮
        if ((buttonAuth || buttonAuthFlag)) {
            KeyValue keyValue = new KeyValue(OperateButtonEnum.FINANCE_CREDIT_UPDATE_BUTTON.getCode(), OperateButtonEnum.FINANCE_CREDIT_UPDATE_BUTTON.getValue());
            operateButton.add(keyValue);
        }
        if (!CollectionUtils.isEmpty(supplierCreditList)) {
            operateButton.add(new KeyValue(OperateButtonEnum.FINANCE_CREDIT_DETAIL_BUTTON.getCode(), OperateButtonEnum.FINANCE_CREDIT_DETAIL_BUTTON.getValue()));
            for (SupplierCreditListDto creditListDto : supplierCreditList) {
                creditListDto.setOperateButton(operateButton);
            }
        }

    }

    /**
     * 设置融资列表 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setFinanceTicketOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue());
            keyValues.add(keyValue);
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }
}
